package com.webdesign.action;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.webdesign.service.ITbAnnounceService;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


@Controller("AnnounceAction")
@Scope("prototype")
public class AnnounceAction {
	private ITbAnnounceService itbAnnounceService;
	private String title;
	private String content;
	private Byte type;
	private Boolean flag;
	
	private Map root = new HashMap();
	
	private String propath = ServletActionContext.getRequest().getRealPath("");
	
	private String path = ServletActionContext.getRequest().getContextPath();
	
	private Configuration cfg = new Configuration();
	
	public String submitAnnounce() throws IOException, TemplateException{
		flag=itbAnnounceService.submitAnnounce(title, content, type);
		return"success";
	}
	
	public String noticeCheck() throws IOException, TemplateException{
		cfg.setDirectoryForTemplateLoading(new File(propath + "/template"));
		root.put("path", path);
		Template template = cfg.getTemplate("/noticedetail.ftl");
		Writer out = new OutputStreamWriter(System.out); 
		template.process(root, out);
		out.flush();
		return "success";
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}

	public Byte getType() {
		return type;
	}

	public void setType(Byte type) {
		this.type = type;
	}

	public ITbAnnounceService getItbAnnounceService() {
		return itbAnnounceService;
	}
	
	@Resource(name="TbAnnounceService")
	public void setItbAnnounceService(ITbAnnounceService itbAnnounceService) {
		this.itbAnnounceService = itbAnnounceService;
	}
	public Boolean getFlag() {
		return flag;
	}
	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	public Map getRoot() {
		return root;
	}

	public void setRoot(Map root) {
		this.root = root;
	}
	

}
